import { baseURL } from "~/request";
import { fetchPostForm } from "~/request/request";
import { REP } from "../common";
import { IUploadFile } from "./types";
export interface FileResult {
  file_id: number;
  path: string;
}
const fetchUploadFile = (params: IUploadFile): Promise<REP<FileResult>> =>
  fetchPostForm(`/file/singleUpload`, params);

export const fetchUploadFileWithDeal = (
  params: IUploadFile
): Promise<FileResult> => {
  return new Promise<FileResult>((resolve, reject) => {
    fetchUploadFile(params).then((r) => {
      if (r && r.code !== 1000) {
        reject(false);
      } else {
        resolve({
          path: baseURL + r.result.path,
          file_id: r.result.file_id,
        });
      }
    });
  });
};

export const downloadFile = (file_id: string) =>
  `${baseURL}/file/download/${file_id}`;
